﻿using AutoFacPack;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using Models;
using PublicType;
using Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DBDesign
{
    public partial class MainForm : XtraForm
    {
        private DevExpress.XtraBars.Docking.DockPanel dockPanel_DataBase = new DevExpress.XtraBars.Docking.DockPanel();
        private DevExpress.XtraBars.Docking.ControlContainer dockPanel_DataBase_Container = new DevExpress.XtraBars.Docking.ControlContainer();
        private TreeList dbManager = new TreeList();

        private ClassStringService ClassStringService { get; set; }
        private ConnectionEntityService ConnectionEntityService { get; set; }
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadLeftDbManager();

            Dictionary<String, String> keyValues = this.ClassStringService.ToClassStringList();



            ReturnData<List<DbElementEntity>> returnData = this.ClassStringService.GetTableInfoList();

            if (returnData.IsSuccess)
            {
                for (int i = 0; i < returnData.Data.Count; i++)
                {
                    if (returnData.Data[i].DbElementType == DbElementType.Table)
                    {
                        ReturnData<List<DbTableFieldEntity>> returnDataColumns = this.ClassStringService.GetColumnInfosByTableName(returnData.Data[i].Name);
                    }
                }
            }

        }


        #region 数据库管理 
        /// <summary>
        /// 数据库管理基础加载
        /// </summary>
        private void LoadLeftDbManager()
        {
            dockManager1.AddPanel(DevExpress.XtraBars.Docking.DockingStyle.Left, dockPanel_DataBase);
            dockPanel_DataBase.Text = "数据库管理";
            dockPanel_DataBase.Controls.Add(dockPanel_DataBase_Container);
            this.Controls.Add(dockPanel_DataBase);

            
            dbManager.ViewStyle = TreeListViewStyle.TreeView;
            dbManager.Dock = DockStyle.Fill;
            dockPanel_DataBase_Container.Controls.Add(dbManager);

            dbManager.TreeLineStyle = DevExpress.XtraTreeList.LineStyle.Wide;
            dbManager.MenuManager = this.DbTreeListMenuManager;
            dbManager.MouseClick += DbManager_MouseClick;
            TreeListColumn treeListColumn = new TreeListColumn();
            treeListColumn.FieldName = "Text";
            treeListColumn.Caption = "数据库";
            treeListColumn.Visible = true;
            treeListColumn.Name = "Name";
            treeListColumn.VisibleIndex = 0;
            treeListColumn.ColumnEditName = "Text";
            treeListColumn.OptionsColumn.AllowEdit = false;
            //是否允许移动    
            treeListColumn.OptionsColumn.AllowMove = false;

            treeListColumn.UnboundType = DevExpress.XtraTreeList.Data.UnboundColumnType.String;
            dbManager.Columns.Add(treeListColumn);
            dbManager.ParentFieldName = "ParentId";
            dbManager.KeyFieldName = "Id";
            dbManager.OptionsMenu.EnableColumnMenu = false;
            dbManager.OptionsMenu.EnableNodeMenu = false;
            dbManager.OptionsMenu.EnableFooterMenu = false;
            dbManager.ImageIndexFieldName = "Id";

            LoadDbMangerData();
        }

        private async void LoadDbMangerData()
        {
            List<TextItem> items = new List<TextItem>();
            for (int i = 0; i < 10; i++)
            {
                TextItem textItem = new TextItem();
                textItem.Text = "aaa" + i;
                textItem.Name = "bbbb" + i;
                textItem.ParentId = "";
                textItem.Id = i.ToString();
                items.Add(textItem);

            }
            for (int i = 0; i < 10; i++)
            {
                TextItem textItem = new TextItem();
                textItem.Text = "aaa" + i;
                textItem.Name = "bbbb" + i;
                textItem.ParentId = i.ToString();
                textItem.Id = (10 + i).ToString();
                items.Add(textItem);
            }

            List<DbConnectionEntity> dbConnectionEntities = await ConnectionEntityService.GetAllDbConnectionEntity();
            if (dbConnectionEntities != null)
            {
                foreach (DbConnectionEntity dbConnectionEntity in dbConnectionEntities)
                {
                    TextItem textItem = new TextItem()
                    {
                        Text = dbConnectionEntity.Text,
                        Id = dbConnectionEntity.Id.ToString(),
                        ParentId = "-1",
                        Tag = dbConnectionEntity
                    };
                    items.Add(textItem);
                }
            }
            dbManager.DataSource = null;
            dbManager.DataSource = items;
        }

        private void DbManager_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeList treeList = sender as TreeList;
                int top = dockPanel_DataBase.Height - dockPanel_DataBase_Container.Height + this.Top + e.Y + treeList.Top + treeList.Parent.Top + treeList.Parent.Parent.Top;
                DbTreeListMenu.ShowPopup(new Point(e.X + this.Left, top));
            }
        }
        private async void DbTreeListMenu_AddContion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DbConnectionEdit dbConnectionEdit = AutoFacEngine.Resolve<DbConnectionEdit>();

            //DbConnectionEdit dbConnectionEdit = new DbConnectionEdit();
            DialogResult dialogResult = dbConnectionEdit.ShowDialog();
            if (dialogResult== DialogResult.OK)
            {
                DbConnectionEntity connectionEntity = dbConnectionEdit.ConnectionEntity;
                connectionEntity.ClassificationId = -1;
                await ConnectionEntityService.AddConnectionEntity(connectionEntity);
                LoadDbMangerData();
            }
            else
            {

            }
            //ConnectionEntity connectionEntity = new ConnectionEntity();

        }
        #endregion

    }
}
